(function(){
	var TRANSACTIONID = new Date().getTime();
	var REQUEST_TIMEOUT = 10000;
	var SERVER = window.location.protocol + '//' +window.location.host+ '/json/Data/';
	var DEV_SERVER = 'http://211.239.159.41:10000/json/Data/';
	var REQUEST_DATA = {
		'CHANNELLIST' : {url: 'ChannelList.asp'},
		'PROGRAMLIST' : {url: 'ProgramList.asp'},
		'EPISODELIST' : {url: 'EpisodeList.asp'},
		'EPISODESTREAMINGURL' : {url: 'EpiSodeStreamingUrl.asp'},
		'PROGRAMCONTENTSLIST' : {url: 'ProgramContentsList.asp'},
		'EPISODELIKE' : {url: 'EpisodeLike.asp'},
		'EPISODESTATICS' : {url: 'EpisodeStatics.asp'},
		'PLAYLIST' : {url: 'PlayList.asp'},
		'PAYMENTLIST' : {url: 'PaymentList.asp'},
		'NOTICE' : {url: 'Notice.asp'},
		'BANNERLIST' : {url: 'BannerList.asp'},
		'EVENTLIST' : {url: 'EventList.asp'},
		'EVENTINSERT' : {url: 'EventInsert.asp'},
		'EVENTDETAIL' : {url: 'EventDetail.asp'},
		'PROMOTIONLIST' : {url: 'PromotionList.asp'},
		'PROMOTIONINSERT' : {url: 'promotionInsert.asp'},
		'PROMOTIONDETAIL' : {url: 'PromotionDetail.asp'},
		'POLL' : {url: 'Poll.asp'},
		'POLLINSERT' : {url: 'PollInsert.asp'},
		'POLLRESULT' : {url: 'PollResult.asp'},
		'ENDEVENTLIST' : {url: 'EndEventList.asp'},
		'CHANNELING' : {url: 'Channeling.asp'},
		'SEARCH' : {url: 'Search.asp'},
		'BUY' : {url: 'Buy.asp'},
		'CONNECTIONLOGINSERT' : {url: 'ConnectionLogInsert.asp'},
		'RETURNCHANNELCODE' : {url: 'ReturnChannelCode.asp'},
		'ANNOUNCELIST' : {url: 'AnnounceList.asp'},
		'CONTENSPAYMENT' : {url: 'ContensPayment.asp'},
		'CONTENTSINFO' : {url: 'ContentsInfo.asp'},
		'RETURNCHANNELCODE' : {url: 'ReturnChannelCode.asp'},
	};

	
	function Ajax (apiObj) {
		var target_url, param_data;
		if (CJ.IS_TV) {
			target_url = SERVER + apiObj.url;
			param_data = apiObj.param;
		} else {
			//PC 개발용 (애뮬레이터는 대응안됨)
			target_url = DEV_SERVER + apiObj.url+ "?"+ $.param(apiObj.param);
			target_url = 'proxy.asp?url=' + encodeURIComponent(DEV_SERVER + apiObj.url+ "?"+ $.param(apiObj.param));
			param_data = '';
		}
		var xhr = $.ajax({
			type: apiObj.method,
			url: target_url,
			data: param_data,
			timeout: REQUEST_TIMEOUT,
			method: 'GET',
			dataType: 'json',
			success: function (jsonObj, textStatus, jqXHR) {
				console.log('success :' + apiObj.url);
				if (jsonObj.resultcode == "200" || jsonObj.resultcode == "300") {
					console.log(jsonObj.message + ': ' + apiObj.url);
				}
				if (typeof apiObj.success == 'function') {
					apiObj.success(jsonObj);
				}
			},
			error:  function (jqXHR, textStatus, errorThrown) {
				console.log('error: ' + apiObj.url);
				if (typeof apiObj.error == 'function') {
					apiObj.error(jqXHR, textStatus);
				}
			},
			complete: function (jqXHR, textStatus) {
				if (typeof apiObj.complete == 'function') {
					apiObj.complete(jqXHR, textStatus);
				}
			}
		});
	}

	function options (obj){
		var preObj = REQUEST_DATA[obj.type];
		for( var p in preObj) {
			obj[p] = preObj[p];
		}
		if (!('param' in obj)) {
			obj.param = {};
		}
		obj.param.transactionId = TRANSACTIONID;
		return obj;
	}


	var HttpJson = {
		get: function(obj) {
			var newOption = options(obj);
			Ajax(newOption);
		},
		set: function(type) {
			var opt = options(type);
			Ajax(opt);
		}
	};
	if (!window.CJ) {
		window.CJ = {};
	}
	window.CJ.HttpJson = HttpJson;
})();